package com.example.taobaounion.ui.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.taobaounion.R;
import com.example.taobaounion.model.bean.IBaseInfo;
import com.example.taobaounion.model.bean.ILinearItemInfo;
import com.example.taobaounion.utils.UrlUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
//分类详情适配器
public class LinearItemContentAdapter extends RecyclerView.Adapter<LinearItemContentAdapter.InnerHolder> {
    //创建集合的好处就是外面数据置空了，里面也不会崩溃
    List<ILinearItemInfo> mData =new ArrayList<>();
    private OnItemClickListener mItemClickListener=null;

    @Override
    public LinearItemContentAdapter.InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //LogUtils.d(this,"打印onCreateViewHolder");
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_linear_goods_content, parent, false);

        return new InnerHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull LinearItemContentAdapter.InnerHolder holder, int position) {
        //LogUtils.d(this,"onBindViewHolder"+position);
        ILinearItemInfo dataBean = mData.get(position);
        //设置数据
        holder.setData(dataBean);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mItemClickListener!=null){
                    mItemClickListener.onItemClick(mData.get(position));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public void setData(List<? extends ILinearItemInfo> contents) {//list传递要看具体类型，不像多态
        //LogUtils.d(this,"contents-->"+contents);
        mData.clear();
        mData.addAll(contents);
        //  notifyDataSetChanged()会将数据更新并重新从适配器里载布局
        notifyDataSetChanged();
    }

    public void addData(List<? extends ILinearItemInfo> contents) {
        //添加之前拿到原来的size
        int olderSize=mData.size();
        mData.addAll(contents);
        //更新UI(比如mData.size()是10个,0到9则加载更多是第10个开始)
        notifyItemRangeChanged(olderSize,contents.size());//两个参数第一个是第几个开始，第二个是数量

    }

    public class InnerHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.goods_cover)
        public ImageView cover;
        @BindView(R.id.goods_title)
        public TextView title;
        @BindView(R.id.goods_off_prise)
        public TextView offPriseTv;
        @BindView(R.id.goods_after_off_prise)
        public TextView finalPriseTv;
        @BindView(R.id.goods_original_prise)
        public TextView originalPriseTv;
        @BindView(R.id.goods_sell_count)
        public TextView sellCountTv;
        public InnerHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }

        public void setData(ILinearItemInfo dataBean) {
            Context context = itemView.getContext();
            title.setText(dataBean.getTitle());
            ViewGroup.LayoutParams layoutParams = cover.getLayoutParams();
            int width=layoutParams.width;
            int height=layoutParams.height;
            //LogUtils.d(this,"width-->"+width);
            //LogUtils.d(this,"height-->"+height);
            //LogUtils.d(this,"url===>"+dataBean.getPict_url());
            Glide.with(context).load(UrlUtils.getCoverPath(dataBean.getCover())).into(cover);
            String finalPrice = dataBean.getFinalPrise();
            long couponAmount = dataBean.getCouponAmount();
            //LogUtils.d(this,"result prise--->"+finalPrice);
            float resultPrise = Float.parseFloat(finalPrice) - couponAmount;
            //LogUtils.d(this,"result prise-->"+resultPrise);
            finalPriseTv.setText(String.format("%.2f",resultPrise));
            offPriseTv.setText(String.format(context.getString(R.string.text_goods_off_prise),dataBean.getCouponAmount()));
            originalPriseTv.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            originalPriseTv.setText(String.format(context.getString(R.string.text_goods_original_prise),finalPrice));
            sellCountTv.setText(String.format(context.getString(R.string.text_goods_sell_count),dataBean.getVolume()));
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener){
        this.mItemClickListener=listener;
    }
    public interface OnItemClickListener{
        void onItemClick(IBaseInfo item);
    }
}
